﻿using System;
using System.Collections.Generic;
using System.Text;
using Com.Guocz.Media.FileSystem;

namespace BVision
{
    class FrmMainLogic
    {
        public IFileSystem create(string name, string pass, string path)
        {
            MetaInfo meta = getMetaInfo(path);
            IFileSystem fs = FileSystem.Create(name, pass, meta);
            return fs;
        }

        private static MetaInfo getMetaInfo(string path)
        {
            MetaInfo meta = new MetaInfo();
            if (path.EndsWith(".bn"))
            {
                path = path.Replace(".bn", "");
            }
            meta.IndexFile = path + ".bn";
            meta.DataFemale = path + ".db";
            meta.DataMale = path + ".dll";
            return meta;
        }

        internal void mkdir(Model model, string dir)
        {
            string path = model.CurrentDir.AbsolutePath;
            if (dir.StartsWith(Const.SEPARATOR))
            {
                path = dir;
            }
            else
            {
                path += Const.SEPARATOR + dir;
            }

            model.FS.mkdirs(path);

            model.CurrentDir = model.FS.getPath(path);
            model.CurrentFile = null;
        }

        internal IFileSystem open(string user, string pass, string path)
        {
            MetaInfo meta = getMetaInfo(path);
            return FileSystem.Open(user, pass, meta);
        }

        internal void addFile(IFileSystem ifs, Path father, string f, int prefixLen)
        {
            string name = f.Substring(prefixLen);
            string interpath = father.AbsolutePath + Const.SEPARATOR + name;
            OutputStream stream = ifs.create(interpath);
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(f, System.IO.FileMode.Open);
                byte[] buf = new byte[Const.BUFFER_SIZE];
                while (fs.Position < fs.Length)
                {
                    int read = fs.Read(buf, 0, buf.Length);
                    stream.Write(buf, 0, read);
                }
                fs.Close();
            }
            finally
            {
                stream.Close();
            }
        }

        internal void addFile(IFileSystem ifs, Path father, string f)
        {
            int prefixLen = f.LastIndexOf('\\') + 1;
            addFile(ifs, father, f, prefixLen);
        }

        internal byte[] readFile(Model model)
        {
            InputStream stream = model.FS.open(model.CurrentFile.AbsolutePath);
            byte[] buf = new byte[Const.BUFFER_SIZE];
            List<byte> allBytes = new List<byte>();

            while (true)
            {
                int read = stream.Read(buf, 0, buf.Length);
                if (read == 0)
                {
                    break;
                }
                for (int i = 0; i < read; i++)
                {
                    allBytes.Add(buf[i]);
                }
            }
            return allBytes.ToArray();
        }

        internal void deleteDir(IFileSystem fs, Path p)
        {
            if (p == null)
            {
                return;
            }

            fs.delete(p.AbsolutePath);
        }

        internal void deleteFile(Model model)
        {
            model.FS.delete(model.CurrentFile.AbsolutePath);
            model.CurrentFile = null;
        }

        internal void preImage(Model model)
        {
            if (model.CurrentDir == null)
            {
                return;
            }
            for (int i = 0; i < model.CurrentDir.Children.Count; i++)
            {
                if (model.CurrentFile.Name.Equals(model.CurrentDir.Children[i].Name))
                {
                    for (i--; i >= 0; i--)
                    {
                        if (!model.CurrentDir.Children[i].IsDirectory)
                        {
                            model.CurrentFile = model.CurrentDir.Children[i];
                            break;
                        }
                    }
                    break;
                }
            }
        }

        internal void nextImage(Model model)
        {
            if (model.CurrentDir == null)
            {
                return;
            }
            for (int i = 0; i < model.CurrentDir.Children.Count; i++)
            {
                if (model.CurrentFile.Name.Equals(model.CurrentDir.Children[i].Name))
                {
                    for (i++; i < model.CurrentDir.Children.Count; i++)
                    {
                        if (!model.CurrentDir.Children[i].IsDirectory)
                        {
                            model.CurrentFile = model.CurrentDir.Children[i];
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }
}
